<!doctype html>
<html lang="zh-CN"><head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=livereload" data-no-instant defer></script>
    <title>薛尧的博客</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="" />

    
    
    
    <link rel="stylesheet" href="../../../css/theme.min.css">

    
    
    
    
    <link rel="stylesheet" href="../../../css/custom.min.css">
    

    
</head>
<body>
        <div id="content" class="mx-auto"><header class="container mt-sm-5 mt-4 mb-4 mt-xs-1">
    <div class="row">
        
        <div class="col-sm-4 col-12 text-sm-right text-center pt-sm-4">
            <a href="../../../" class="text-decoration-none">
                <img id="home-image" class="rounded-circle"
                    
                        src="https://ueyao.github.io/image-hosting/blog/avatar.png"
                    
                />
            </a>
        </div>
        <div class="col-sm-8 col-12 text-sm-left text-center">
        
            <h2 class="m-0 mb-2 mt-4">
                <a href="../../../" class="text-decoration-none">
                    
                        薛尧
                    
                </a>
            </h2>
            <p class="text-muted mb-1">
                
                    Java Developer | Short Video Creator
                
            </p>
            <ul id="nav-links" class="list-inline mb-2">
                
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../about/" title="关于">关于</a>
                    </li>
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../post/" title="文章">文章</a>
                    </li>
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../categories/" title="分类">分类</a>
                    </li>
                
            </ul>
            <ul id="nav-social" class="list-inline">
                
                    <li class="list-inline-item mr-3">
                        <a href="http://github.com/flowstone" target="_blank">
                            <i class="fab fa-github fa-1x text-muted"></i>
                        </a>
                    </li>
                
                    <li class="list-inline-item mr-3">
                        <a href="mailto:xueyao.me#gmail.com" target="_blank">
                            <i class="fas fa-at fa-1x text-muted"></i>
                        </a>
                    </li>
                
            </ul>
        </div>
    </div>
    <hr />
</header>
<div class="container">
    <div class="pl-sm-2">
        <div class="mb-3">
            <h3 class="mb-0">Java多线程-线程状态</h3>
            
            <small class="text-muted">发布于 2019-08-25</small>
        </div>

        <article>
            <h1 id="线程状态">线程状态</h1>
<p>6个状态定义：java.lang.Thread.State</p>
<ol>
<li>New: 尚未启动的线程的线程状态。</li>
<li>Runnable: 可运行线程的线程状态，等待CPU调度。</li>
<li>Blocked: 线程阻塞等待监视器锁定的线程状态。处于synchronized同步代码块或方法中被阻塞。</li>
<li>Waiting: 等待线程的线程状态。下列不带超时的方式：Object.wait、Thread.join、LockSupport.park</li>
<li>Timed Waiting: 具有指定等待时间的等待线程的线程状态。下列超时的方式：Thread.sleep、Object.wait、Thread.join、LockSupport.parkNanos、LockSupport.parkUntil</li>
</ol>
<p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/thread-state-01.png" alt="线程状态"></p>
<h2 id="常见线程状态切换">常见线程状态切换</h2>
<h3 id="新建-运行-终止">新建-&gt;运行-&gt;终止</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>Thread thread1 <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> Thread(<span style="color:#66d9ef">new</span> Runnable() {
</span></span><span style="display:flex;"><span>			<span style="color:#a6e22e">@Override</span>
</span></span><span style="display:flex;"><span>			<span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">run</span>() {
</span></span><span style="display:flex;"><span>				System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;thread1当前状态：&#34;</span> <span style="color:#f92672">+</span> Thread.<span style="color:#a6e22e">currentThread</span>().<span style="color:#a6e22e">getState</span>().<span style="color:#a6e22e">toString</span>());
</span></span><span style="display:flex;"><span>				System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;thread1 执行了&#34;</span>);
</span></span><span style="display:flex;"><span>			}
</span></span><span style="display:flex;"><span>		});
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;没调用start方法，thread1当前状态：&#34;</span> <span style="color:#f92672">+</span> thread1.<span style="color:#a6e22e">getState</span>().<span style="color:#a6e22e">toString</span>());
</span></span><span style="display:flex;"><span>thread1.<span style="color:#a6e22e">start</span>();
</span></span><span style="display:flex;"><span>Thread.<span style="color:#a6e22e">sleep</span>(2000L); <span style="color:#75715e">// 等待thread1执行结束，再看状态</span>
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;等待两秒，再看thread1当前状态：&#34;</span> <span style="color:#f92672">+</span> thread1.<span style="color:#a6e22e">getState</span>().<span style="color:#a6e22e">toString</span>());
</span></span></code></pre></div><p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/thread-state-02.png" alt=""></p>
<h3 id="新建-运行-等待-运行-终止">新建-&gt;运行-&gt;等待-&gt;运行-&gt;终止</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>Thread thread2 <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> Thread(<span style="color:#66d9ef">new</span> Runnable() {
</span></span><span style="display:flex;"><span>			<span style="color:#a6e22e">@Override</span>
</span></span><span style="display:flex;"><span>			<span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">run</span>() {
</span></span><span style="display:flex;"><span>				<span style="color:#66d9ef">try</span> {<span style="color:#75715e">// 将线程2移动到等待状态，1500后自动唤醒</span>
</span></span><span style="display:flex;"><span>					Thread.<span style="color:#a6e22e">sleep</span>(1500);
</span></span><span style="display:flex;"><span>				} <span style="color:#66d9ef">catch</span> (InterruptedException e) {
</span></span><span style="display:flex;"><span>					e.<span style="color:#a6e22e">printStackTrace</span>();
</span></span><span style="display:flex;"><span>				}
</span></span><span style="display:flex;"><span>				System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;thread2当前状态：&#34;</span> <span style="color:#f92672">+</span> Thread.<span style="color:#a6e22e">currentThread</span>().<span style="color:#a6e22e">getState</span>().<span style="color:#a6e22e">toString</span>());
</span></span><span style="display:flex;"><span>				System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;thread2 执行了&#34;</span>);
</span></span><span style="display:flex;"><span>			}
</span></span><span style="display:flex;"><span>		});
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;没调用start方法，thread2当前状态：&#34;</span> <span style="color:#f92672">+</span> thread2.<span style="color:#a6e22e">getState</span>().<span style="color:#a6e22e">toString</span>());
</span></span><span style="display:flex;"><span>thread2.<span style="color:#a6e22e">start</span>();
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;调用start方法，thread2当前状态：&#34;</span> <span style="color:#f92672">+</span> thread2.<span style="color:#a6e22e">getState</span>().<span style="color:#a6e22e">toString</span>());
</span></span><span style="display:flex;"><span>Thread.<span style="color:#a6e22e">sleep</span>(200L); <span style="color:#75715e">// 等待200毫秒，再看状态</span>
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;等待200毫秒，再看thread2当前状态：&#34;</span> <span style="color:#f92672">+</span> thread2.<span style="color:#a6e22e">getState</span>().<span style="color:#a6e22e">toString</span>());
</span></span><span style="display:flex;"><span>Thread.<span style="color:#a6e22e">sleep</span>(3000L); <span style="color:#75715e">// 再等待3秒，让thread2执行完毕，再看状态</span>
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;等待3秒，再看thread2当前状态：&#34;</span> <span style="color:#f92672">+</span> thread2.<span style="color:#a6e22e">getState</span>().<span style="color:#a6e22e">toString</span>());
</span></span></code></pre></div><p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/thread-state-03.png" alt=""></p>
<h3 id="新建-运行-阻塞-运行-终止">新建-&gt;运行-&gt;阻塞-&gt;运行-&gt;终止</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span>Thread thread3 <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> Thread(<span style="color:#66d9ef">new</span> Runnable() {
</span></span><span style="display:flex;"><span>			<span style="color:#a6e22e">@Override</span>
</span></span><span style="display:flex;"><span>			<span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">run</span>() {
</span></span><span style="display:flex;"><span>				<span style="color:#66d9ef">synchronized</span> (Demo2.<span style="color:#a6e22e">class</span>) {
</span></span><span style="display:flex;"><span>					System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;thread3当前状态：&#34;</span> <span style="color:#f92672">+</span> Thread.<span style="color:#a6e22e">currentThread</span>().<span style="color:#a6e22e">getState</span>().<span style="color:#a6e22e">toString</span>());
</span></span><span style="display:flex;"><span>					System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;thread3 执行了&#34;</span>);
</span></span><span style="display:flex;"><span>				}
</span></span><span style="display:flex;"><span>			}
</span></span><span style="display:flex;"><span>		});
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">synchronized</span> (Demo2.<span style="color:#a6e22e">class</span>) {
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;没调用start方法，thread3当前状态：&#34;</span> <span style="color:#f92672">+</span> thread3.<span style="color:#a6e22e">getState</span>().<span style="color:#a6e22e">toString</span>());
</span></span><span style="display:flex;"><span>    thread3.<span style="color:#a6e22e">start</span>();
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;调用start方法，thread3当前状态：&#34;</span> <span style="color:#f92672">+</span> thread3.<span style="color:#a6e22e">getState</span>().<span style="color:#a6e22e">toString</span>());
</span></span><span style="display:flex;"><span>    Thread.<span style="color:#a6e22e">sleep</span>(200L); <span style="color:#75715e">// 等待200毫秒，再看状态</span>
</span></span><span style="display:flex;"><span>    System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;等待200毫秒，再看thread3当前状态：&#34;</span> <span style="color:#f92672">+</span> thread3.<span style="color:#a6e22e">getState</span>().<span style="color:#a6e22e">toString</span>());
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>Thread.<span style="color:#a6e22e">sleep</span>(3000L); <span style="color:#75715e">// 再等待3秒，让thread3执行完毕，再看状态</span>
</span></span><span style="display:flex;"><span>System.<span style="color:#a6e22e">out</span>.<span style="color:#a6e22e">println</span>(<span style="color:#e6db74">&#34;等待3秒，让thread3抢到锁，再看thread3当前状态：&#34;</span> <span style="color:#f92672">+</span> thread3.<span style="color:#a6e22e">getState</span>().<span style="color:#a6e22e">toString</span>());
</span></span></code></pre></div><p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/thread-state-04.png" alt=""></p>

        </article>
    </div>

    <div id="disqus_thread"></div>
<script>
    window.disqus_config = function () {
    
    
    
    };
    (function() {
        if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
            document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
            return;
        }
        var d = document, s = d.createElement('script'); s.async = true;
        s.src = '//' + "xie-yao-de-bo-ke" + '.disqus.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

            </div>
        </div><footer class="text-center pb-1">
    <small class="text-muted">
        
        &copy; Copyright 2024
        
        |
        <a href="https://beian.miit.gov.cn/" target="_blank">苏ICP备14012079号</a>
        <br />
        由 <a href="https://gohugo.io/" target="_blank">Hugo</a> 强力驱动
        | 主题 <a href="https://github.com/austingebauer/devise" target="_blank">Devise</a>
        <br />

    </small>
</footer></body>
</html>
